<?php
namespace home\Model;
use Think\Model;
class UserModel extends Model{

    public function parse_users($users,$type=2){
        //$type=1详细模式 $type=2简洁模式
        foreach ($users as $key => $value) {
            $users[$key] = $this->parse_user($users[$key],$type);
        }
        return $users;
    }

    public function parse_user($user,$type=1){

        //$type=1详细模式 $type=2简洁模式

        if ($type==1){
            $lv = $this->get_lv($user['exp']);
            $user['lv'] = $lv['lv'];
            $user['need_exp'] = $lv['exp'];
            $user['exp_percent'] = $lv['percent'];

            $user['follow_number'] = M('collection')->where('user_id = '.$user['id'].' and type = 2')->count();
            $user['fans_number'] = M('collection')->where('object_id = '.$user['id'].' and type = 2')->count();
            $user['business_card_number'] = M('business_card')->where('give_user_id = '.$user['id'])->count();

            $user['money4'] = M('money_log')->where('type = 4 and user_id = '.$user['id'])->sum("change_value");
            if ($user['money4'] == null){
                $user['money4'] = 0.00;
            }
            $user['bbs_number'] = M('content')->where('class_id = 3 and status = 1 and user_id = '.$user['id'])->count();
            $user['ask_number'] = M('content')->where('class_id = 1 and status = 1 and user_id = '.$user['id'])->count();
            $user['answer_number'] = M('content')->where('class_id = 2 and status = 1 and user_id = '.$user['id'])->count();

            $url = $user['bg_img']."?x-oss-process=image/average-hue";
            $user['bg_color'] = substr(json_decode(file_get_contents($url),true)['RGB'],2) ;
        }


        $user['tags'] = json_decode($user['tags'],true);
        $user['shop_fk_uuids'] = json_decode($user['shop_fk_uuids'],true);
        $user['topic_ids'] = json_decode($user['topic_ids'],true);

        if ($user['school_start_time'] != null){
            $user['school_start_time_str'] = date("Y-m-d",$user['school_start_time']);
        }
        

        if ($user['school_end_time'] != null){
            $user['school_end_time_str'] = date("Y-m-d",$user['school_end_time']);
            $user['work_year'] = intval((time() - $user['school_end_time'])/86400/365);
        }else{
            $user['work_year'] = 0;
        }


        if ($user['city_name'] == null){
            $user['city_name'] = "未选城市";
        }


        $topic_names = array();
        foreach ($user['topic_ids'] as $key => $value) {
            $name = M('type')->where('id = '.$user['topic_ids'][$key])->find()['name'];
            array_push($topic_names,$name);
        }
        $user['topic_names'] = $topic_names;

        
        


        foreach ($user as $key => $value) {
            if ($user[$key] === NULL){
                $user[$key] = "";
            }
        }

        return $user;
    }

    public function get_user_list($type=1){
        $users = M('user as a')->join('left join city as b on a.city_id = b.id')
            ->field("a.* , b.name as city_name")->select();
        return $this->parse_users($users);
    }

    public function get_user_info($id,$type=1,$user_id="")
    {
        //$type=1详细模式 $type=2简洁模式
        $user = M('user as a')->join('left join city as b on a.city_id = b.id')
            ->field("a.* , b.name as city_name")
            ->where('a.uuid = "'.$id.'"')->find();
        if ($user == null) {
            return false;
        }
        if ($user_id != ""){
            $user['is_collection'] =
                M('collection')->where('user_id = '.$user_id.' and object_id = '.$user['id'].' and type = 2')
                    ->count();
        }else{
            $user['is_collection'] = 0;
        }


        $user = $this->parse_user($user,$type);

        return $user;

    }



    public function get_lv($exp)
    {
        $exps = M('lv')->order('lv asc')->select();
        foreach ($exps as $key => $value) {
            if($exps[$key]['exp'] > $exp){
                $data['lv'] = $exps[$key-1]['lv'];
                $data['exp'] = $exps[$key]['exp'];
                $data['percent'] = intval(($exp/$data['exp'])*100);
                return $data;
            }
        }
    }

}


